/**
 * Created by KaZeline on 15-4-27.
 */
// $("#txtStartTime_b").bind("click focus", function () {  
	   // var endtimeTf = $dp.$('txtEndTime_b');             
	   // WdatePicker({                            
		   // maxDate: '#F{$dp.$D(\'txtEndTime_b\')}',      
		   // dateFmt: "yyyy-MM-dd",              
		   // onpicked: function () { endtimeTf.focus(); }          
		// });         
	// });          
	// $("#txtEndTime_b").bind("click focus", function () {       
		// WdatePicker({           
		   // minDate: '#F{$dp.$D(\'txtStartTime_b\')}', 
		   // dateFmt: "yyyy-MM-dd"       
	   // });         
	// });
$(".time_li").each(function(){
	if($(this).hasClass("active")){
		var t = $(this).attr("data-time");
		search(t);
	}
})
$(".time_li").on("click",function(){
	var t = $(this).attr("data-time");
	search(t);
});
$("#sub").click(function(){
	var bt = $("#txtStartTime_c").val();
	var et = $("#txtEndTime_c").val();
	//$(".time_le").text(bt);
	//$(".time_te").text(et);
	$(".time_show_a").css("display","none");
	searcha(bt,et);
})
var getStartTime = function(){
    if($('input:radio[name="dateTrigger"]:checked')){return $('input:radio[name="dateTrigger"]:checked').val();}     
};
var getType = function(){
    return $("#cType").val();
};
var search = function(t){
	//var t = $(this).attr("data-time");
	$(".time_le").text(t);
    var startTime = t;
    var cType = getType();
    if(!startTime){
        console && console.log("缺省startTime");
        return false;
    }
    if(!cType){
        console && console.log("缺省cType");
        return false;
    }
    PFT_GLOBAL.G.Ajax({
        url : "http://www.12301.cc/module/zax/baiduline/get_ticket.php",
        data : {btime3:startTime,type:cType},
        dataType : "json",
        type :'GET',
        timeout : function(){console && console.log("获取数据超时")},
        serverError : function(){console &&
        console.log("接口出错www.12301.cc/module/zax/baiduline/get_ticket.php")}
    },function(res){
        _refresh(res)
    });
}
var getStartTimea = function(){
    if($('input:radio[name="dateTrigger"]:checked')){return $('input:radio[name="dateTrigger"]:checked').val();}

        
};
var getTypea = function(){
    return $("#cType").val();
};
var searcha = function(e,b){
	
	//var bt = $("#txtStartTime_c").val();
	//var et = $("#txtEndTime_c").val();
    var startTime = e;
    var endTime = b;
    var cType = getTypea();
    if(!startTime){
        console && console.log("缺省startTime");
        return false;
    }
	if(!endTime){
        console && console.log("缺省startTime");
        return false;
    }
    if(!cType){
        console && console.log("缺省cType");
        return false;
    }
    PFT_GLOBAL.G.Ajax({
        url : "http://www.12301.cc/module/zax/baiduline/get_ticket.php",
		data : {btime3:startTime,etime3:endTime,type:cType},
        dataType : "json",
        type :'GET',
        timeout : function(){console && console.log("获取数据超时")},
        serverError : function(){console &&
        console.log("接口出错www.12301.cc/module/zax/baiduline/get_ticket.php")}
    },function(res){
        _refresh(res)
    });
}
$(".time_li_a").on("click",function(){
	$(".time_none_a").css("display","block");
	$(".time_show_a").css("display","none");
});
$(".weeka").click(function(){
	$(this).addClass("active");
	$(".moutha").removeClass("active");
	$(".springa").removeClass("active");
	$(".wangt").removeClass("active");
	$(".time_none_a").css("display","none");
})
$(".moutha").click(function(){
	$(this).addClass("active");
	$(".weeka").removeClass("active");
	$(".springa").removeClass("active");
	$(".wangt").removeClass("active");
	$(".time_none_a").css("display","none");
})
$(".springa").click(function(){
	$(this).addClass("active");
	$(".weeka").removeClass("active");
	$(".moutha").removeClass("active");
	$(".wangt").removeClass("active");
	$(".time_none_a").css("display","none");
	
})
$(".wangt").click(function(){
	$(this).addClass("active");
	$(".weeka").removeClass("active");
	$(".moutha").removeClass("active");
	$(".springa").removeClass("active");
})

function  _refresh(res){
    require.config({
        packages: [
            {
                name: 'echarts',
                location: 'http://echarts.baidu.com/src',
                main: 'echarts'
            },
            {
                name: 'zrender',
                location: 'http://www.12301.cc/module/zax/baiduline/map_2',
                main: 'zrender'
            }
        ],
        paths: {
            echarts: 'http://echarts.baidu.com/doc/example/www/js',
            theme  : 'http://echarts.baidu.com/doc/example/theme'
        }
    });
    var option = {
        tooltip : {
            trigger: 'item'
        },
        toolbox: {
            show : true,
            orient: 'vertical',
            x:'right',
            y:'center',
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false}
            }
        },
        series : [
            {
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}'
                },
                name: '选择器',
                type: 'map',
                mapType: 'china',
                mapLocation: {
                    x: 'left',
                    y: 'top',
                    width: '30%'
                },
                roam: true,
                selectedMode : 'single',
                itemStyle:{
                    emphasis:{label:{show:true}}
                },
                data:[
                    {name: '北京', selected:false},
                    {name: '天津', selected:false},
                    {name: '上海', selected:false},
                    {name: '重庆', selected:false},
                    {name: '河北', selected:false},
                    {name: '河南', selected:false},
                    {name: '云南', selected:false},
                    {name: '辽宁', selected:false},
                    {name: '黑龙江', selected:false},
                    {name: '湖南', selected:false},
                    {name: '安徽', selected:false},
                    {name: '山东', selected:false},
                    {name: '新疆', selected:false},
                    {name: '江苏', selected:false},
                    {name: '浙江', selected:false},
                    {name: '江西', selected:false},
                    {name: '湖北', selected:false},
                    {name: '广西', selected:false},
                    {name: '甘肃', selected:false},
                    {name: '山西', selected:false},
                    {name: '内蒙古', selected:false},
                    {name: '陕西', selected:false},
                    {name: '吉林', selected:false},
                    {name: '福建', selected:false},
                    {name: '贵州', selected:false},
                    {name: '广东', selected:false},
                    {name: '青海', selected:false},
                    {name: '西藏', selected:false},
                    {name: '四川', selected:false},
                    {name: '宁夏', selected:false},
                    {name: '海南', selected:false},
                    {name: '台湾', selected:false},
                    {name: '香港', selected:false},
                    {name: '澳门', selected:false}
                ]
            }
        ],
        animation: false
    };

    var city_data=[];
    for (var i in res.city){
        city_data[i] = {name:res.city[i].city,value:res.city[i].torder};
    }
    require(
    [
        'echarts',
        'theme/gray',
        'echarts/chart/map'
    ],
    function (ec,theme) {
        var myChart = ec.init(document.getElementById('main'),theme);
        myChart.showLoading();
        myChart.setOption(option);
        var ecConfig = require('echarts/config');
        myChart.on(ecConfig.EVENT.MAP_SELECTED, function (param){

            var selected = param.selected;
            var selectedProvince;
            var name;
            for (var i = 0, l = option.series[0].data.length; i < l; i++) {
                name = option.series[0].data[i].name;
                option.series[0].data[i].selected = selected[name];
                if (selected[name]) {
                    selectedProvince = name;
                }
            }
            if (typeof selectedProvince == 'undefined') {
                option.series.splice(1);
                option.legend = null;
                option.dataRange = null;
                myChart.setOption(option, true);
                return;
            }
            option.series[1] = {
                name: '订单数量',
                type: 'map',
                mapType: selectedProvince,
                itemStyle:{
                    normal:{label:{show:true}},
                    emphasis:{label:{show:true}}
                },
                mapLocation: {
                    x: '35%'
                },
                roam: true
            };

            option.series[1]['data'] = city_data;
            option.legend = {
                x:'right',
                data:['订单数量']
            };
            option.dataRange = {
                orient: 'horizontal',
                x: 'right',
                min: 0,
                max: 150,
                color:['red','yellow'],
                text:['高','低'],
                splitNumber:0
            };
            myChart.hideLoading();
            myChart.setOption(option, true);
        });
    });
}

